﻿using DataServer.ApplicationLib.Services;
using DataServer.CommonLib.Constants;
using DataServer.CommonLib.Models.DataBase;
using Microsoft.AspNetCore.Mvc;

namespace Config.WebApi.Controllers
{
    [ApiController]
    public class DatabaseController : ControllerBase
    {
        private readonly IDatabaseService _databaseService;

        public DatabaseController(IDatabaseService databaseService)
        {
            _databaseService = databaseService;
        }

        [HttpGet(ApiRoutes.Database.QueryDatabases)]
        public async Task<IActionResult> QueryDatabases([FromQuery] DatabaseSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _databaseService.QueryDatabasesByPageAsync(model);
                return Ok(data);
            }
            else
            {
                var data = await _databaseService.QueryDatabasesAsync(model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.Database.CreateDatabase)]
        public async Task<IActionResult> CreateDatabase(DatabaseEditRequestDto model)
        {
            var data = await _databaseService.CreateDatabaseAsync(model);
            return Ok(data);
        }

        [HttpGet(ApiRoutes.Database.GetDatabase)]
        public async Task<IActionResult> GetDatabase(Guid id)
        {
            var data = await _databaseService.GetDatabaseAsync(id);
            return Ok(data);
        }

        [HttpPut(ApiRoutes.Database.UpdateDatabase)]
        public async Task<IActionResult> UpdateDatabase(Guid id, [FromBody] DatabaseEditRequestDto model)
        {
            var data = await _databaseService.UpdateDatabaseAsync(id, model);

            return Ok(data);
        }

        [HttpGet(ApiRoutes.Database.ConnDatabase)]
        public async Task<IActionResult> TestConnDatabase(Guid id)
        {
            var data = await _databaseService.TestConnDatabaseAsync(id);
            return Ok(data);
        }
    }
}